program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;
type
  pelement = ^element;
  element = record
    value: integer;
    next: pelement;
  end;
procedure AddToBegin (var head: pelement; num: integer);
var tmp: pelement;
begin
  tmp:= head;
  new(head);
  head^.value:= num;
  head^.next:= tmp; 
end;
function AddeFromBegin(var head: pelement): integer;
var
  tmp: pelement;
begin
  if (head<>nil) then
  begin
    tmp:= head^.next;
    dispose(head);
    head:= tmp;
  end;
end;
procedure AddeFromEnd(head: pelement);
var
  current, tmp: pelement;
begin
  if (head<>nil) and (head^.next<>nil) then
  begin
    current:= head;
    if current^.next^.next<>nil then
    begin
      current:= current^.next;
    end else begin
      tmp:= nil;
      dispose(current^.next^.next);
      current^.next:= tmp;
    end;
  end;
end;
procedure PrintList(head: pelement);
var
  current: pelement;
begin
  current:= head;
  while (current<>nil) do
  begin
    writeln(current^.value);
    current:=current^.next;
  end;
end;
var
  i, n, num: integer;
  fin: textfile;
  head, tmp, current: pelement;
begin
  assignfile(fin, 'run.txt');
  reset(fin);
  readln(fin, n);
  for I := 1 to n do
  begin
    readln(fin, num) ;
    AddToBegin(head, num);
  end;
  for I := 1 to 5 do
  begin
    AddeFromBegin(tmp);
  end;
  for I := 1 to n do
  begin
    AddeFromEnd(current);
  end;
  PrintList(current);
  readln;
  current:=head;
  if (head<>nil) and (current^.next<>nil) and (current^.next^.next<>nil) and (current^.next^.next^.next<>nil) then
  begin
    tmp:= current^.next^.next^.next^.next;
    dispose(current^.next^.next^.next);
    current^.next^.next:= tmp;
  end;
end.
